home *** CD-ROM | disk | FTP | other *** search
Text File | 1996-08-16 | 11.0 KB | 424 lines | [TEXT/MPS ] |
- //========================================================================================
- //
- // File: FWCmd.cpp
- // Release Version: $ ODF 1 $
- //
- // Copyright: (c) 1993 - 1996 by Apple Computer, Inc., all rights reserved.
- //
- //========================================================================================
-
- #include "FWFrameW.hpp"
-
- #ifndef FWCMD_H
- #include "FWCmd.h"
- #endif
-
- // ----- Framework Includes -----
-
- #ifndef FWPART_H
- #include "FWPart.h"
- #endif
-
- #ifndef FWFRAME_H
- #include "FWFrame.h"
- #endif
-
- #ifndef FWSELECT_H
- #include "FWSelect.h"
- #endif
-
- #ifndef FWPRESEN_H
- #include "FWPresen.h"
- #endif
-
- // ----- OS Layer -----
-
- #ifndef FWMENU_K
- #include "FWMenu.k"
- #endif
-
- #ifndef FWRESACC_H
- #include "FWResAcc.h"
- #endif
-
- #ifndef FWRESTYP_H
- #include "FWResTyp.h"
- #endif
-
- #ifndef SLODFSTR_K
- #include "SLODFStr.k"
- #endif
-
- #ifndef SLODFSTR_H
- #include "SLODFStr.h"
- #endif
-
- // ----- Foundation Layer -----
-
- #ifndef FWCFMRES_H
- #include "FWCFMRes.h"
- #endif
-
- #ifndef FWMEMMGR_H
- #include "FWMemMgr.h"
- #endif
-
- #ifndef FWSTRS_H
- #include "FWStrs.h"
- #endif
-
- #ifndef FWBNDSTR_H
- #include "FWBndStr.h"
- #endif
-
- // ----- OpenDoc Includes -----
-
- // for ODIText
- #ifndef _ITEXT_
- #include "IText.h"
- #endif
-
- #ifndef SOM_Module_OpenDoc_Commands_defined
- #include <CmdDefs.xh>
- #endif
-
- #ifndef SOM_ODPart_xh
- #include "Part.xh"
- #endif
-
- #ifndef SOM_ODFrame_xh
- #include "Frame.xh"
- #endif
-
- #ifndef SOM_ODStorageUnit_xh
- #include <StorageU.xh>
- #endif
-
- #ifndef SOM_ODSession_xh
- #include <ODSessn.xh>
- #endif
-
- // ----- Platform Includes -----
-
- #if defined(FW_BUILD_MAC) && !defined(__SCRIPT__)
- #include <Script.h>
- #endif
-
- #if defined(FW_BUILD_WIN32S) && !defined(__WINNT_H)
- #include <winnt.h>
- #endif
-
-
- //========================================================================================
- // Runtime Info
- //========================================================================================
-
- #ifdef FW_BUILD_MAC
- #pragma segment odfcommands
- #endif
-
- FW_DEFINE_AUTO(FW_CCommand)
-
- //========================================================================================
- // FW_CCommand class
- //========================================================================================
-
- //----------------------------------------------------------------------------------------
- // FW_CCommand constructor
- //----------------------------------------------------------------------------------------
-
- FW_CCommand::FW_CCommand(Environment* ev,
- ODCommandID id,
- FW_CFrame* frame,
- FW_Boolean canUndo) :
- fCommandID(id),
- fCanUndo(canUndo),
- fCausesChange(TRUE),
- fActionType(kODSingleAction),
- fPart(NULL),
- fFrame(frame),
- fUndoString(NULL),
- fRedoString(NULL),
- fUndo(NULL),
- fHasAddedAction(FALSE)
- {
- FW_ASSERT(fFrame);
-
- fPart = fFrame->GetPart(ev);
- fPart->GetODPart(ev)->Acquire(ev);
-
- // [HLX] ATTENTION: Because secondary frames may have dissepear during Undo
- // we need to use the source frame
- fSourceFrame = fFrame->GetSourceFrame(ev);
-
- fPresentation = fFrame->GetPresentation(ev);
- fUndo = fPart->GetSession(ev)->GetUndo(ev);
-
- FW_END_CONSTRUCTOR
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CCommand destructor
- //----------------------------------------------------------------------------------------
-
- FW_CCommand::~FW_CCommand()
- {
- FW_START_DESTRUCTOR
-
- FW_SOMEnvironment ev;
- fPart->GetODPart(ev)->Release(ev);
-
- PrivDisposeUndoStrings();
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CCommand::PrivDisposeUndoStrings
- //----------------------------------------------------------------------------------------
-
- void FW_CCommand::PrivDisposeUndoStrings()
- {
- if (fUndoString)
- ::DisposeIText(fUndoString);
- if (fRedoString)
- ::DisposeIText(fRedoString);
-
- fUndoString = fRedoString = NULL;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CCommand::Execute
- //----------------------------------------------------------------------------------------
- // Note: Execute returns true if the command is on the undo stack. It returns false if the
- // command has been deleted because it was not undoable
-
- FW_Boolean FW_CCommand::Execute(Environment* ev, FW_Boolean deleteIfNotUndoable)
- {
- FW_Boolean wasDone = FALSE;
- FW_Boolean isOnUndoStack = TRUE;
- FW_VOLATILE(wasDone);
-
- FW_TRY
- {
- this->DoIt(ev);
- wasDone = TRUE;
-
- if (fCausesChange) // ??? It might be a good idea to rethrow here
- fPart->Changed(ev); // in case we're linking
-
- if (GetCanUndo(ev))
- AddAction(ev, fActionType, (octet*) &this, sizeof(FW_CCommand*), GetUndoString(ev), GetRedoString(ev));
- }
- FW_CATCH_BEGIN
- FW_CATCH_EVERYTHING()
- {
- if (!wasDone)
- {
- CommitDone(ev);
- delete this;
- FW_THROW_SAME();
- }
-
- // Don't rethrow if the command was completed
- SetCanUndo(ev, FALSE);
- }
- FW_CATCH_END
-
- // If the command has not been added to the action stack then kill it
-
- if (!HasAddedAction(ev))
- {
- CommitDone(ev);
- if (deleteIfNotUndoable)
- delete this;
- isOnUndoStack = FALSE;
- }
-
- return isOnUndoStack;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CCommand::UndoIt
- //----------------------------------------------------------------------------------------
-
- void FW_CCommand::UndoIt(Environment* ev)
- {
- FW_UNUSED(ev);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CCommand::RedoIt
- //----------------------------------------------------------------------------------------
-
- void FW_CCommand::RedoIt(Environment* ev)
- {
- FW_UNUSED(ev);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CCommand::CommitDone
- //----------------------------------------------------------------------------------------
-
- void FW_CCommand::CommitDone(Environment* ev)
- {
- // Called by FW_CPart::DisposeActionState before deleting the command
- // The command has been done, and possibly redone
-
- if (GetCanUndo(ev))
- this->FreeUndoState(ev);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CCommand::CommitUndone
- //----------------------------------------------------------------------------------------
-
- void FW_CCommand::CommitUndone(Environment* ev)
- {
- // Called by FW_CPart::DisposeActionState before deleting the command
- // The last action for this command was an Undo
-
- if (GetCanUndo(ev))
- this->FreeRedoState(ev);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CCommand::SaveUndoState
- //----------------------------------------------------------------------------------------
- void FW_CCommand::SaveUndoState(Environment* ev)
- {
- FW_UNUSED(ev);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CCommand::SaveRedoState
- //----------------------------------------------------------------------------------------
- void FW_CCommand::SaveRedoState(Environment* ev)
- {
- FW_UNUSED(ev);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CCommand::FreeUndoState
- //----------------------------------------------------------------------------------------
- void FW_CCommand::FreeUndoState(Environment* ev)
- {
- FW_UNUSED(ev);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CCommand::FreeRedoState
- //----------------------------------------------------------------------------------------
- void FW_CCommand::FreeRedoState(Environment* ev)
- {
- FW_UNUSED(ev);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CCommand::SetMenuStrings
- //----------------------------------------------------------------------------------------
-
- void FW_CCommand::SetMenuStrings(Environment* ev,
- const FW_CString& undoString,
- const FW_CString& redoString)
- {
- FW_UNUSED(ev);
- PrivDisposeUndoStrings();
- fUndoString = ::CopyIText(undoString.RevealODIText());
- fRedoString = ::CopyIText(redoString.RevealODIText());
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CCommand::SetMenuStrings
- //----------------------------------------------------------------------------------------
-
- void FW_CCommand::SetMenuStrings(Environment* ev,
- char* undoChars,
- char* redoChars)
- {
- FW_CString255 undoString(undoChars);
- FW_CString255 redoString(redoChars);
- this->SetMenuStrings(ev, undoString, redoString);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CCommand::SetMenuStringsFromResource
- //----------------------------------------------------------------------------------------
- void FW_CCommand::SetMenuStringsFromResource(Environment* ev,
- short stringResourceID,
- short undoStringID,
- short redoStringID)
- {
- FW_CSharedLibraryResourceFile resFile(ev);
- FW_CString undoString;
- FW_CString redoString;
- ::FW_LoadStringByID(ev, resFile, stringResourceID, MULTISTRINGRES, undoStringID, undoString);
- ::FW_LoadStringByID(ev, resFile, stringResourceID, MULTISTRINGRES, redoStringID, redoString);
- this->SetMenuStrings(ev, undoString, redoString);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CCommand::AddAction
- //----------------------------------------------------------------------------------------
-
- void FW_CCommand::AddAction(Environment* ev,
- ODActionType actionType,
- octet* dataPtr,
- unsigned long dataSize,
- ODName* undoActionLabel,
- ODName* redoActionLabel)
- {
- // Set up the action data record
- ODActionData actionState;
- actionState._maximum = dataSize;
- actionState._length = dataSize;
- actionState._buffer = dataPtr;
-
- fUndo->AddActionToHistory(ev, fPart->GetODPart(ev),
- &actionState,
- actionType,
- undoActionLabel,
- redoActionLabel);
- if (dataSize != 0)
- fHasAddedAction = TRUE; // OpenDoc has a ptr to this command object
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CCommand::IsOKtoEdit
- //----------------------------------------------------------------------------------------
-
- FW_Boolean FW_CCommand::IsOKtoEdit(Environment* ev)
- {
- FW_Boolean result = TRUE;
-
- if (fCausesChange)
- {
- if (fPart->IsReadOnly(ev))
- result = FALSE;
- else
- {
- // Check the frame's link status to see if editing is allowed
- FW_ASSERT(fFrame != NULL);
- ODLinkStatus status = fFrame->GetODFrame(ev)->GetLinkStatus(ev);
- if (status == kODInLinkDestination)
- {
- // uh-oh, make sure Part::EditInLinkAttempted is called
- result = fFrame->GetODFrame(ev)->EditInLink(ev);
- }
- }
- }
-
- return result;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CCommand::PrivLoadDefaultUndoStrings
- //----------------------------------------------------------------------------------------
-
- void FW_CCommand::PrivLoadDefaultUndoStrings(Environment* ev)
- {
- FW_CString undoString;
- FW_CString redoString;
-
- ::FW_PrivLoadDefaultUndoStrings(ev, undoString, redoString);
- SetMenuStrings(ev, undoString, redoString);
- }
-